import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/InfoServlet")
public class InfoServlet extends HttpServlet {
    private static final String URL = "jdbc:mysql://localhost:3306/novaldata?useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "Hiyori";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String query = "SELECT UID, nickname, email FROM userdata WHERE username = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, username);
                ResultSet resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    request.setAttribute("UID", resultSet.getString("UID"));
                    request.setAttribute("nickname", resultSet.getString("nickname"));
                    request.setAttribute("email", resultSet.getString("email"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.getRequestDispatcher("/info/info.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");

        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String query = "UPDATE userdata SET nickname = ?, email = ? WHERE username = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, nickname);
                preparedStatement.setString(2, email);
                preparedStatement.setString(3, username);
                int result = preparedStatement.executeUpdate(); // 获取更新的行数
                if (result > 0) {
                    // 更新成功后，更新 session 中的昵称和邮箱
                    session.setAttribute("nickname", nickname);
                    session.setAttribute("email", email);
                    // 重定向到 doGet 方法
                    response.sendRedirect(request.getContextPath() + "//InfoServlet");
                } else {
                    response.sendRedirect("../info/info.jsp?uid=" + username + "&error=No rows updated");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            response.sendRedirect("../info/info.jsp?uid=" + username + "&error=Database error");
        }
    }
}